﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace RSALicCore
{
    /// <summary>
    /// 公钥授权管理器
    /// </summary>
    public class PubLicManager : PriLicManager
    {
        /// <summary>
        /// 构造一个管理器
        /// </summary>
        /// <param name="pubkey"></param>
        public PubLicManager(string pubkey) : base(pubkey)
        {
        }

        /// <summary>
        /// 根据给定的数据，生成授权数据
        /// </summary>
        /// <param name="data">给定的数据</param>
        /// <returns>授权数据</returns>
        public override string MakeLic(string data)
        {
            var hash = Convert.FromBase64String(data);
            var cipherbytes = rsa.Encrypt(hash, fOAEP);
            return Convert.ToBase64String(cipherbytes);
        }

        /// <summary>
        /// 验证授权
        /// </summary>
        /// <param name="lic">授权</param>
        /// <param name="oirData">原始对比数据</param>
        /// <returns>验证结果</returns>
        public bool VertifyLic(string lic, string oirData)
        {
            var data = Convert.FromBase64String(oirData);

            RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(rsa);
            deformatter.SetHashAlgorithm(HashAlgorithm);

            byte[] rgbSignature = Convert.FromBase64String(lic);

            return deformatter.VerifySignature(data, rgbSignature);
        }
    }
}
